package com.bm.greeting.config;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bm.greeting.entity.EntertainmentNews;
import com.bm.greeting.entity.Weather;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jinmy
 * @date 2022-08-04 22:58
 */
public class ToolsUtils {


    public static String getCaiHongPi() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(BaseInfoConfig.CAI_HONG_PI);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        String content = newslist.getJSONObject(0).getString(BaseInfoConfig.CONTENT);
        return content;
    }


    public static Map<String, String> getLaoHuangLi() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(BaseInfoConfig.LAO_HUANG_LI + "&date=2023-09-01");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.RESULT);
        JSONObject data = newslist.getJSONObject(0);
        Map mapType = JSON.parseObject(data.toJSONString(),Map.class);
        return mapType;
    }

    public static JSONArray getStar() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(BaseInfoConfig.STAR + "&astro=水瓶座");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.RESULT);
        JSONArray data = newslist.getJSONObject(0).getJSONArray("list");
        return data;
    }

    public static Map<String, String> getEnsentence() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(BaseInfoConfig.ENSENTENCE);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }

            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        String en = newslist.getJSONObject(0).getString(BaseInfoConfig.EN);
        String zh = newslist.getJSONObject(0).getString(BaseInfoConfig.ZH);
        Map<String, String> map = new HashMap<>(16);
        map.put(BaseInfoConfig.ZH, zh);
        map.put(BaseInfoConfig.EN, en);
        return map;
    }

    public static String getTheTrickOfLife() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(BaseInfoConfig.QIAO_MEN);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        String content = newslist.getJSONObject(0).getString(BaseInfoConfig.CONTENT);
        return content;
    }

    public static Map<String, String> getQingShi() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(BaseInfoConfig.QING_SHI);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        String content = newslist.getJSONObject(0).getString(BaseInfoConfig.CONTENT);
        String source = newslist.getJSONObject(0).getString(BaseInfoConfig.SOURCE);
        String author = newslist.getJSONObject(0).getString(BaseInfoConfig.AUTHOR);
        Map<String, String> map = new HashMap<>(16);
        map.put(BaseInfoConfig.CONTENT, content);
        map.put(BaseInfoConfig.SOURCE, source);
        map.put(BaseInfoConfig.AUTHOR, author);
        return map;
    }


    public static Map<String, String> getPyqWenan() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(BaseInfoConfig.PYQ_WENAN);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        String content = newslist.getJSONObject(0).getString(BaseInfoConfig.CONTENT);
        Map<String, String> map = new HashMap<>(16);
        map.put(BaseInfoConfig.CONTENT, content);
        return map;
    }

    public static String getGoodMorning() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(BaseInfoConfig.GOODMORNING);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        String content = newslist.getJSONObject(0).getString(BaseInfoConfig.CONTENT);
        return content;
    }

    public static Weather getWeather() {

        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> map = new HashMap<String, String>();
        //TODO 长春行政代码
        map.put(BaseInfoConfig.DISTRICT, BaseInfoConfig.DISTRICT_ID);
        //TODO 数据类型
        map.put(BaseInfoConfig.DATA_TYPE, BaseInfoConfig.DATA_TYPE_VALUE);
        map.put(BaseInfoConfig.AK, BaseInfoConfig.AK_VALUE);
        String res = restTemplate.getForObject(
                BaseInfoConfig.WATER,
                String.class,
                map);
        JSONObject json = JSONObject.parseObject(res);
        JSONArray forecasts = json.getJSONObject(BaseInfoConfig.RESULT).getJSONArray(BaseInfoConfig.FORECASTS);
        List<Weather> weathers = forecasts.toJavaList(Weather.class);
        JSONObject now = json.getJSONObject(BaseInfoConfig.RESULT).getJSONObject(BaseInfoConfig.NOW);
        Weather weather = weathers.get(0);
        weather.setText_now(now.getString(BaseInfoConfig.TEXT));
        weather.setTemp(now.getString(BaseInfoConfig.TEMP));
        weather.setWind_class(now.getString(BaseInfoConfig.WIND_CLASS));
        weather.setWind_dir(now.getString(BaseInfoConfig.WIND_DIR));
        return weather;
    }

    public static List<EntertainmentNews> getNews() {

        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(BaseInfoConfig.NEWS);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod(BaseInfoConfig.GET);
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, BaseInfoConfig.UTF));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray forecasts = jsonObject.getJSONArray(BaseInfoConfig.NEWSLIST);
        List<EntertainmentNews> entertainmentNews = forecasts.toJavaList(EntertainmentNews.class);
        return entertainmentNews;
    }


}
